package com.token;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * token工具类
 */
public class GetAndSetToken {

    /**
     * 从请求头中获取token并在响应头中保存token
     */
    public static String getAndSetToken(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        if(token==null){
            //产生一个临时token，在未登录时使用，登录后替换
            token = JwtUtil.generToken("code",null,null);
        }
        //响应头也存在跨域
        response.addHeader("Access-Control-Expose-Headers","token");
        //发送给客户端
        response.addHeader("token",token);
        return token;
    }
}
